//
//  PhotoViewController.m
//  LimitFree
//
//  Created by LZXuan on 15-6-26.
//  Copyright (c) 2015年 轩哥. All rights reserved.
//

#import "PhotoViewController.h"
#import "UIImageView+WebCache.h"

@interface PhotoViewController () <UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>
{
    UICollectionView *_collectionView;
    NSMutableArray *_dataArr;
}
@property (nonatomic,strong) UICollectionView *collectionView;
@property (nonatomic,strong) NSMutableArray *dataArr;
@end

@implementation PhotoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self dataInit];
    [self creatCollectionView];
}
- (void)dataInit {
    self.dataArr = [[NSMutableArray alloc] init];
    for (NSDictionary *dict in self.photos) {
        [self.dataArr addObject:dict[@"originalUrl"]];
    }
    //有数据了 刷新视图
    [self.collectionView reloadData];
}
- (void)creatCollectionView {
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    //cell 的固定大小
    layout.itemSize = CGSizeMake(kScreenSize.width, kScreenSize.height-64);
    //两个cell 之间 水平和竖直最小间隔0
    layout.minimumInteritemSpacing = 0;
    layout.minimumLineSpacing = 0;
    //水平方向
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, kScreenSize.width, kScreenSize.height-64) collectionViewLayout:layout];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    //按页滚动
    self.collectionView.pagingEnabled = YES;
    
    [self.view addSubview:self.collectionView];
    //注册cell
    [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"CollectionViewCell"];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    //让集合视图滚动到 指定的cell
    //第0分区第self.selectIndex 个cell
    [self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:self.selectIndex inSection:0] atScrollPosition:UICollectionViewScrollPositionNone animated:YES];
}


//有多少分区
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)  collectionView {
    return 1;
}
//每个分区有多少cell/item
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.dataArr.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CollectionViewCell" forIndexPath:indexPath];
    
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, collectionView.frame.size.width, collectionView.frame.size.height)];
    [imageView sd_setImageWithURL:[NSURL URLWithString:self.dataArr[indexPath.row]] placeholderImage:[UIImage imageNamed: @"Default1"]];
    
    cell.backgroundView = imageView;
    
    return cell;
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
